import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:rapid_kit/rapid_kit.dart';

class QualityPicker {
  final BuildContext context;
  final int selectedQualityIndex;
  final Function(int) onQualitySelected;

  QualityPicker({
    required this.context,
    required this.selectedQualityIndex,
    required this.onQualitySelected,
  });

  void show(List<Quality> qualities) {
    showModalBottomSheet(
      context: context,
      builder: (BuildContext context) {
        return Wrap(
          children: [
            Column(
              mainAxisSize: MainAxisSize.min,
              children: List.generate(qualities.length, (index) {
                return _qualityItem(index, qualities, context);
              }),
            ),
          ],
        );
      },
    );
  }

  Widget _qualityItem(
      int index, List<Quality> qualities, BuildContext context) {
    return Column(
      children: [
        ListTile(
          leading: Icon(
            selectedQualityIndex == index
                ? Icons.check_circle
                : Icons.circle_outlined,
            color: selectedQualityIndex == index ? Colors.blue : Colors.grey,
          ),
          title: Text(qualities[index].label == 'SD'
              ? '标清'
              : qualities[index].label == 'HD'
                  ? '高清'
                  : qualities[index].label == 'FHD'
                      ? '超高清'
                      : qualities[index].label == 'UHD'
                          ? '4k'
                          : '未至'),
          onTap: () {
            onQualitySelected(index);
            Navigator.pop(context);
          },
        ),
        if (index < qualities.length - 1) const Divider(),
      ],
    );
  }
}
